<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=edge" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<title>手写apply</title>
	</head>
	<body>
		<script>
			Function.prototype.myApply = function (context, args) {
				//这里默认不传就是给window,也可以用es6给参数设置默认参数
				context = context || window
				args = args ? args : []
				//给context新增一个独一无二的属性以免覆盖原有属性
				const key = Symbol()
				context[key] = this
				//通过隐式绑定的方式调用函数
				const result = context[key](...args)
				//删除添加的属性
				delete context[key]
				//返回函数调用的返回值
				return result
			}
			var name = 'geyao'
			var fangfang = {
				name: 'fangfang',

				fang: function () {
					console.log(this.name)
				},

				fun: function () {
                    console.log(this,"this")
					setTimeout(
						function () {
							this.fang()
						}.myApply(this),
						100
					)
				},
			}
			fangfang.fun() //fangfang
		</script>
	</body>
</html>
